﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Jinjia.Approve.Entity;
using Jinjia.Approve.IRepository;
using Jinjia.Approve.Model.VM.Org;
using Jinjia.Core.UnitOfWork.UnitOfWorkComponent;
using SqlSugar;

namespace Jinjia.Approve.Repository
{
    public class ErpaUserRepository : IErpaUserRepository
    {
        private readonly ISqlSugarClient _db;

        public ErpaUserRepository(IUnitOfWork unitOfWork)
        {
            _db = unitOfWork.GetDbClient();
        }

        public async Task<erpa_user_tbl> GetById(int id)
        {
            return await _db.Queryable<erpa_user_tbl>().FirstAsync(m => m.id == id);
        }

        public async Task<List<erpa_user_tbl>> GetListAsync(Expression<Func<erpa_user_tbl, bool>> expression)
        {
            return await _db.Queryable<erpa_user_tbl>().Where(expression).ToListAsync();
        }


        public async Task<List<erpa_user_tbl>> GetUsersByDingTalkUserIds(List<string> userIds)
        {
            return await _db.Queryable<erpa_user_tbl>().In(x => x.ding_user_id, userIds).ToListAsync();
        }
        
        /// <summary>
        /// 获取用户上级部门
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="departmentId">部门id</param>
        /// <returns></returns>
        public UserParentOrg GetUserParentOrg(int userId = 0, string departmentId = "0")
        {
            string sql = @"SELECT
                                eut.id                  AS UserId,
                                fdt.name                 AS DepartmentName,
                                fdt.department_id        AS DepartmentId,
                                fdt.parent_department_id AS ParentDepartmentId,
                                fut.name                 AS LeaderName,
                                fut.mid                  AS LeaderUserId,
                                fut2.mid                 AS ParentLeaderUserId
                            FROM
                                erpa_user_tbl eut
                                    JOIN erpa_relationship_tbl ert ON eut.id = ert.user_id AND ert.deleted = 0
                                    JOIN feishu_department_tbl fdt ON fdt.mid = ert.department_id AND fdt.deleted = 0
                                    LEFT JOIN feishu_user_tbl fut ON fut.user_id = fdt.leader_user_id
                                    LEFT JOIN feishu_department_tbl fdt2 ON fdt.parent_department_id = fdt2.department_id AND fdt2.deleted = 0
                                    LEFT JOIN feishu_user_tbl fut2 ON fut2.user_id = fdt2.leader_user_id
                            WHERE
                                  TRUE ";

            if (userId != 0)
            {
                sql += $" AND eut.id = {userId} ";
            }
            
            if (departmentId != "0")
            {
                sql += $" AND fdt.department_id = '{departmentId}' ";
            }
            
            sql += " ORDER BY fdt.parent_department_id ";

            return _db.Ado.SqlQuery<UserParentOrg>(sql).FirstOrDefault();
        }
    }
}